<?php
/**
 * A thread interface
 *
 * This file is allowed to only access a db.interface if neccesary
 * and itself. 
 *
 **/
  

// XXX : turn off error through when not debugging
// XXX : the set methods need input validation for SQL inj.

require 'db.php';

/** UTIL FNS **/

// TODO
function clean( $string ) {
  return $string;
}

function openDbConn() {
  global $db_name, $db_user, $db_pass, $db_options; 
  return new PDO($db_name, $db_user, $db_pass, $db_options);
}

/** ACCESS FNS **/

// int -> int -> obj?
function getPosts($threadNum, $start, $end) {
  try {
    $db = openDbConn();
    return $db->query("SELECT post.name, post.datum, post.rank, post.date, post.tid 
                         FROM post
                           WHERE post.tid = $threadNum 
                             AND post.rank BETWEEN $start AND $end
                         ORDER BY rank;");
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
// int -> int -> obj
function getPost($threadNum, $postRank) {
  try {
    $db = openDbConn();
    return $db->query("SELECT post.name, post.datum, post.tid, post.rank
                         FROM post
                           WHERE post.tid = $threadNum 
                             AND post.rank = $postRank;")->fetch();
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

// int -> int -> obj?
/* TODO XXX
SELECT thread.rank, thread.mode, post.name, post.datum, MIN(post.rank), MAX(post.date) FROM post, thread WHERE thread.id = post.tid ORDER BY thread.rank DESC
*/
function getThreads($foraNum, $start, $end) {
  try {
    $db = openDbConn();
    return $db->query("SELECT
                         thread.id AS id,
                         thread.mode AS mode,
                           (SELECT inpost.name
                              FROM post AS inpost
                                WHERE post.tid = inpost.tid
                                  AND inpost.rank = 1) AS name,
                           (SELECT inpost.datum
                              FROM post AS inpost
                                WHERE post.tid = inpost.tid
                                  AND inpost.rank =1) AS datum,
                         MAX(post.date)
                       FROM thread
                         INNER
                           JOIN post
                             ON post.tid = thread.id
                             WHERE thread.fid = $foraNum
                       GROUP BY thread.id
                       ORDER BY date ASC;");
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
// int -> int -> obj?
function getFora( $foraNum, $start, $end ) {
  try {
    $db = openDbConn();
    return $db->query("SELECT id, name, datum, rank FROM forum
                          WHERE forum.rank >= $start
                            AND forum.rank <= $end 
                          ORDER BY rank;");
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

// int -> int -> string -> string -> bool
function setPost( $threadNum, $postNum, $title, $datum, $options ) {
  try {
    $mode = ($options['mode']) ? $options['mode'] : "none";
    $time = time();

    // XXX Handle time and date
    $db = openDbConn();
    $db->beginTransaction();
    // create thread for post -1
    if ( $postNum > 0 ) {  
      $db->exec("UPDATE post 
                                    SET name = '$title', 
                                        datum = '$datum',
                                        date = $time 
                                    WHERE rank = $postNum
                                      AND tid = $threadNum;");
    } elseif ( $postNum == 0 ) {
      $db->exec("INSERT INTO thread (fid, mode, rank) 
                                    VALUES ( ${threadNum}, '$mode', 0 );
                                  INSERT INTO post (tid, uid, rank, name, datum, date) 
                                    VALUES( (SELECT MAX(id) FROM thread), 
                                            0,
                                            1 + COALESCE( (SELECT MAX(rank) FROM post
                                                             WHERE tid = (SELECT MAX(id) FROM thread ) ),
                                                      0 ), 
                                            '$title', 
                                            '$datum',
                                            $time);");
    } elseif ( $postNum < 0 ) {
      $db->exec("INSERT INTO post (tid, uid, rank, name, datum, date) 
                                    VALUES( ${threadNum}, 
                                            0,
                                            1+ COALESCE( (SELECT MAX(rank) FROM post
                                                             WHERE tid = (SELECT MAX(id) FROM thread ) ),
                                                      0 ), 
                                            '$title', 
                                            '$datum',
                                            $time);");     
    }
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print 'ERROR: ' . $e->getMessage() . '<br />';
    die();
  }
}

// XXX remove the crux of this
// int -> int -> string -> string -> bool
function setThread( $threadNum, $mode ) {
  try {
    $db = openDbConn();
    $db->beginTransaction();
    $db->exec("UPDATE thread SET mode = $mode WHERE id = $threadNum");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
// int -> int -> string -> string -> bool
// city num as of current is ignored
function setFora( $cityNum, $forumNum, $title, $datum) {
  try {
    $db = openDbConn();
    $db->beginTransaction();
    ( $threadNum < 0 ) ? $db->exec("INSERT INTO forum (rank, name, datum)
                                      VALUES( 1 + (SELECT MAX(rank) FROM forum),
                                              $title,
                                              $datum);")
                       : $db->exec("UPDATE forum
                                      SET datum = $datum, name = $name
                                      WHERE id = $forumNum;");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}

// int -> int
function postCount($threadNum) {
  try {
    $db = openDbConn();
    // XXX does this work? I don't member
    $res = $db->query("SELECT COUNT(rank) AS rank FROM post WHERE tid = $threadNum;"); 
    $cnt = $res['rank'];
    $res = NULL;
    return $cnt;
  } catch ( PDOException $e ) {
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
}
// int -> int
function threadCount( $foraNum ) {
}


?>

